from turbogears import expose
from mnemoply._renewed_lib.crud import CRUDController, ItemTab,TableCol 
from mnemoply.model.field import Group as model_Group, GroupSearch 

class Group1Controller(CRUDController):

    model_object = model_Group
    mysql_view_name = 'prod.group_lookup'
    controller_name = "Group_new"
    search_model = GroupSearch
    filter_by_user = ['office','faculty']
    lookup_columns = [
        TableCol(name='faculty', caption=_("Faculty")),
        TableCol(name='title', caption=_("Name"), size=14),
        TableCol(name='education_type', caption=_("Initial plan")),
        TableCol(name='teacher', caption=_("Teacher"), size=13),
        TableCol(name='students_left', caption=_("Seats left"), size=4, ),
        TableCol(name='year', caption=_("Year"), size=4),
        TableCol(name='office', caption=_("Office"), size=10, hidden=True),
        TableCol(name='max_students', caption=_("Max students"), size=4, hidden=True,),
        TableCol(name='cache_students', caption=_("Number of students"), size=4, hidden=True,),
        TableCol(name='cache_schedule_days', caption=_("Schedule days"), hidden=True),
        TableCol(name='start_date', caption=_("Start date"), hidden=True),
        TableCol(name='end_date', caption=_("End date"), hidden=True),
        TableCol(name='is_open_str', caption=_("State"), hidden=True),
        TableCol(name='with_agreement', caption=_("With agreement"), hidden=True),
        TableCol(name='session', caption=_("Session"), hidden=True),
        TableCol(name='replacement1', caption=_("Replacement 1"), hidden=True),
        TableCol(name='replacement2', caption=_("Replacement 2"), hidden=True),
        TableCol(name='comment', caption=_("Comment"), hidden=True),
    ]
    add_fields = [
        'officeID',
        'initialPlanID',
        'organizationID',
        'customerTariffID',
        'teacherID', 'replacement1ID', 'replacement2ID',
        'maxStudents', 'lessonPeriod',
        'startDate',
        'comment',
        'withAgreement',
        'session',
    ]
    edit_fields = ['ordinal'] + add_fields
    view_fields = edit_fields + ['endDate', 'isOpen']
    edit_fields.remove('initialPlanID')  # hack: remove group.plan from edit fields


    @expose(format="json")
    def find(self, searchString=None,**kw):
        
        return CRUDController.find(self,searchString = searchString,
                    exposed_method = lambda obj: '%s (%d)' % (obj.title, obj.maxStudents - obj.cacheStudents),
                    pre_obj_condition=lambda obj: obj.initialPlan.year == 2013,
                           )
    
exposed = Group1Controller()
